<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: LJQ <245369209@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller\api;

use app\common\controller\Api;
/**
 * 流程管理
 * @package app\enterprise
 */
class Workflow extends Api

{

    /**
   * 流程列表
   * @return  json 
   * 
   *
   */

	public function workflowList(){
        $model=model('common/workflow');
        $where = [] ;
        if(($limit = input('limit')) && ($page = input('page'))){
           	$startLimit =  ($page-1)*$limit;
            $info =model('common/workflow')->with('steps,steps.charge')->where($where)->limit($startLimit.','.$limit)->select();
        }else{
            $info =collection(model('common/workflow')->with('steps,steps.charge')->select())->toArray();
        }
      
            $count = count($info); 
            return json(['code'=>1,'msg'=>'流程信息','count'=>$count,'data'=>$info]);
 	 }
   	  
   	      //添加流程
   	 public function add($data){
           if (empty($data)) {
               return json(['code'=>0,'msg'=>'数据异常,请检查']);
           }
           $model = model('common/workflow');
           if(false == $model->save($data)){
           		 return json(['code'=>0,'msg'=>'添加失败,请检查']);
           }else{
                   setlog('workflowadd','workflow',session('user_auth.id'));
           		return json(['code'=>1,'msg'=>'流程添加成功']);
           }
   	 }
   	
   /**
   * 修改流程
   * @param string workflowId 流程id,
   * @return json
   *
   */
   	public function edit($data = null,$workflowId = null){
        
          if (empty($workflowId)) {
              return json(['code'=>0,'msg'=>'流程不存在,请检查']);
          }	
        $model = model('common/workflow');
    	 
	          if ($model->change($data)) {
               setlog('workflowupdate','workflow',session('user_auth.id'));
	             return json(['code'=>1,'msg'=>'用户修改成功']);
	            }else{
	             return json(['code'=>0,'msg'=>'用户修改失败,请检查']);
	            }
	       
    	  return json(['code'=>0,'msg'=>'用户修改失败,请检查']);
   }

   	 
     
    /**
   *  删除流程
   * @param  string workflowId 用户id,
   * @return  json
   *
   */
     
    public function del($workflowId) {
            $workflowId = input('workflowId',0,'intval');
            $model =  model('common/workflow');

        if (empty ( $workflowId )||empty(($model->where(array('id'=>$workflowId))->find()))) {
           return  json(['code'=>0,'msg'=>'流程id不存在,请检查']);
         }
          
        if(model('Workstep')->destroy(['wfFlowInfoId'=>$workflowId]) !== FALSE && $model->destroy(['id'=>$workflowId]) !== FALSE){
           setlog('workflowdel','workflow',session('user_auth.id'));
         return json(['code'=>1,'msg'=>'流程删除成功']);
       }else{
          return $this->json(['code'=>0,'msg'=>'流程删除失败,请检查']);
       }
    }

    public function search($keyword){
    	$where = [];
    	if($keyword){
    		$where = ['flowName'=>['like','%'.$keyword.'%']];
    	}
    	$info =collection(model('Workflow')->with('steps,steps.charge')->where($where)->select())->toArray();
    	//总数
    	$count = count($info);
    	if (empty($info) && $count == 0) {
    		return json(['code'=>0,'msg'=>'没有找到流程信息','count'=>$count]);
    	}
    	
    	if ($count > 0){
    		return json(['code'=>1,'msg'=>'流程信息','count'=>$count,'data'=>$info]);
    	}
    }


}